package com.dao.Impl;

import com.dao.StudentDao;
import com.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class StudenDaoImpl implements StudentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Student SelectStudentByid(int sid) {
        String sql="select * from student where sid=?";
        try{
            Student student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), sid);
            return student;
        }catch (Exception e){
            return null;
        }
    }

    public List<Student> showAll() {
        String sql="select * from student";
        List<Student> students = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
        return students;
    }

    public int showNums() {
        String sql="select count(*) from student";
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
        return integer;
    }

    public List<Student> showAllPage(int pageNum, int pageSize) {
        String sql="select * from student limit ?,?";
        List<Student> students = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class), (pageNum - 1) * pageSize, pageSize);
        return students;
    }

    public int delStu(int sid) {
        String sql="delete from student where sid=?";
        int i = jdbcTemplate.update(sql, sid);
        return i;
    }

    public int updStu(int sid, String sname, String ssex, Date birth) {
        String sql="update student set sname=?,ssex=?,birth=? where sid=?";
        int i = jdbcTemplate.update(sql, sname, ssex, birth, sid);
        return i;
    }

    public int addStu(String sname, String ssex, Date birth) {
        String sql="insert into student (sid,sname,ssex,birth) values (null,?,?,?)";
        int i = jdbcTemplate.update(sql, sname, ssex, birth);
        return i;
    }
}
